<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <meta name="renderer" content="webkit">
    <title></title>
    <link rel="stylesheet" href="css/pintuer.css">
    <link rel="stylesheet" href="css/admin.css">
    <script src="js/jquery.js"></script>
    <script src="js/pintuer.js"></script>
    <script src="js/commons.js"></script>
    <script src="js/getparameter.js"></script>
</head>
<body>
<div class="panel admin-panel">
    <div class="panel-head"><strong><span class="icon-key"></span> 修改用户</strong></div>
    <div class="body-content">
        <form method="post" class="form-x" action="">

            <div class="form-group">
                <div class="label">
                    <label>登陆账户</label>
                </div>
                <div class="field">
                    <input type="text" class="input w50" id="adminLoginName" name="adminLoginName" size="50"/>
                </div>
            </div>
            <div class="form-group">
                <div class="label">
                    <label>登陆密码</label>
                </div>
                <div class="field">
                    <input type="text" class="input w50" id="userPassword" name="userPassword" size="50"
                           placeholder="请输入新密码..."/>
                </div>
            </div>
            <div class="form-group">
                <div class="label">
                    <label>姓名</label>
                </div>
                <div class="field">
                    <input type="text" class="input w50" id="adminName" name="adminName" size="100"/>
                </div>
            </div>
            <div class="form-group">
                <div class="label">
                    <label>性别</label>
                </div>
                <div class="field">
                    <select id="adminSex" name="adminSex">
                        <option value="1">男</option>
                        <option value="2">女</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <div class="label">
                    <label>电话</label>
                </div>
                <div class="field">
                    <input type="text" class="input w50" id="adminTel" name="adminTel" size="100"/>
                </div>
            </div>
            <div class="form-group">
                <div class="label">
                    <label>职位</label>
                </div>
                <div class="field">
                    <select id="duty" name="dutyName">

                    </select>
                </div>
            </div>
            <div class="form-group">
                <div class="label">
                    <label>代表团</label>
                </div>
                <div class="field">
                    <select id="deputation" name="deputationName">

                    </select>
                </div>
            </div>
            <div class="form-group">
                <div class="label">
                    <label></label>
                </div>
                <div class="field">
                    <input id="submit" class="button bg-main icon-check-square-o" type="button" value="修改">
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    function reset_duty() {
        let option_str = "<option value='0'>--请选择--</option>";
        $("#duty").html(option_str);
    }

    var deputation_id
    $("#deputation").on("change", function () {
        deputation_id = $(this).val();
        ajax_get_duty(deputation_id, true);
    });

    function ajax_get_duty(deputation_id) {
        $.ajax({
            "url": "/MangeUserController/listStaff",
            "type": "POST",
            "data": JSON.stringify({"delegationId": deputation_id}),
            "dataType": "json",
            "contentType": "application/json; charset=utf-8",
            "success": function (resp) {
                if (resp.code == 0) {
                    var staffList = resp.t;
                    let farm_option_str = "<option value='0'>--请选择--</option>";
                    for (let p in staffList) {
                        farm_option_str += "<option value='" + staffList[p].staffId + "'>" + staffList[p].staffName + "</option>"
                    }
                    $("#duty").html(farm_option_str);
                }
            }
        });
    }

    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]);
        return null; //返回参数值
    }

    $(function () {
        $.ajax({
            "url": "/MangeUserController/listDelegation",
            "type": "POST",
            "dataType": "json",
            "contentType": "application/json; charset=utf-8",
            "success": function (resp) {
                 if (resp.code == 0) {
                    var delegationList = resp.t;
                    let farm_option_str = "<option value='0'>--请选择--</option>";
                    for (let p in delegationList) {
                        farm_option_str += "<option value='" + delegationList[p].delegationId + "'>" + delegationList[p].delegationName + "</option>"
                    }
                    $("#deputation").html(farm_option_str);
                    reset_duty();
                }
            }
        });
        $.ajax({
            "url": "/MangeUserController/listAllStaff",
            "type": "POST",
            "dataType": "json",
            "contentType": "application/json; charset=utf-8",
            "success": function (resp) {
                if (resp.code == 0) {
                    var staffList = resp.t;
                    let farm_option_str = "<option value='0'>--请选择--</option>";
                    for (let p in staffList) {
                        farm_option_str += "<option value='" + staffList[p].staffId + "'>" + staffList[p].staffName + "</option>"
                    }
                    $("#duty").html(farm_option_str);
                }
            }
        });
        $.ajax({
            "url": "/MangeUserController/findUserById",
            "type": "POST",
            "data": JSON.stringify({"uid": getUrlParam("uid")}),
            "dataType": "json",
            "contentType": "application/json; charset=utf-8",
            "success": function (resp) {
                if (resp.code == 0) {
                    var user = resp.t;
                    $("#adminLoginName").val(user.userName),
                        $("#userPassword").val(user.password),
                        $("#adminName").val(user.userRealName),
                        $("#adminSex").val(user.gender),
                        $("#deputation").get(0).selectedIndex = user.delegationId,
                        $("#duty").get(0).selectedIndex = user.staffId,
                        $("#deputation").val(user.delegationId),
                        $("#duty").val(user.staffId),
                        $("#adminTel").val(user.telPhone)
                }
            }
        });
    })

    var duty_id
    $("#duty").on("change", function () {
        duty_id = $(this).val();
    });


    $("#submit").click(function () {
        var data = {"uid": getUrlParam("uid"),
            "userName": $("#adminLoginName").val(), "password": $("#userPassword").val(),
            "userRealName": $("#adminName").val(), "gender": $("#adminSex").val(),
            "telPhone": $("#adminTel").val(), "staffId": duty_id,
            "delegationId": deputation_id
        }

        $.ajax({
            "url": "/MangeUserController/updateUser",
            "type": "POST",
            "data": JSON.stringify(data),
            "dataType": "json",
            "contentType": "application/json; charset=utf-8",
            "success": function (resp) {
                if (resp.code == 0) {
                    alert("修改成功")
                } else {
                    alert(resp.msg)
                }
            }
        });
    });
</script>
</body>
</html>